const articleService = require("../services/articleService");

class ArticleController {
  // 创建文章
  async create (ctx) {
    const data = ctx.request.body;
    const user_id = ctx.state.user.user_id;
    await articleService.create(data, user_id);

    ctx.body = {
      code: 200,
      data: null,
      message: '创建成功'
    }
  }

  // 更新文章
  async update (ctx) {
    const data = ctx.request.body;
    const user_id = ctx.state.user.user_id;
    await articleService.update(data, user_id);
    ctx.body = {
      code: 200,
      data: null,
      message: '更新成功'
    }
  }

  // 删除文章
  async delete (ctx) {
    const { id } = ctx.query;
    await articleService.delete(id);
    ctx.body = {
      code: 200,
      data: null,
      message: '删除成功'
    }
  }

  // 获取单个文章
  async findOne (ctx) {
    const { id } = ctx.query;
    const article = await articleService.findOne(id);
    ctx.body = {
      code: 200,
      data: article,
      message: '查询成功'
    }
  }

  // 获取所有文章
  async findAll (ctx) {
    const articles = await articleService.findAll();
    ctx.body = {
      code: 200,
      data: articles,
      message: 'Success'
    }
  }
}

module.exports = new ArticleController();